package com.zqh.springframework;

import com.zqh.app.service.UserService;
import com.zqh.springframework.annotation.Component;

/**
 * @Author: zqh
 * @CreateTime: 2025-10-10
 * @Description: 动态代理实现后置处理器
 * @Version: 1.0
 */
@Component
public class AutoProxyCreatorBeanPostProcessor implements BeanPostProcessor {


    public Object createProxy(Object bean, String beanName)
    {
        // 创建动态代理
        // 匹配切点表达式 ----> aspectj 能力
        if(bean instanceof UserService){
            JdkDynimcProxy jdkDynimcProxy = new JdkDynimcProxy(bean);
            return jdkDynimcProxy.getProxy();
        }

        // 没有实现AOP 就返回原始bean对象
        return bean;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws Exception {
        System.out.println("初始化前拓展。。。。");
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws Exception {
        System.out.println("初始化后拓展。。。。");
        if(bean instanceof UserService){
            System.out.println("创建代理对象");
            JdkDynimcProxy jdkDynimcProxy = new JdkDynimcProxy(bean);
            return jdkDynimcProxy.getProxy();
        }
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }



}
